package cn.tnar.cloud.parkservice.controller;

import cn.tnar.cloud.parkservice.feign.AcctMemberInfoFeign;
import cn.tnar.cloud.parkservice.model.entity.ParkInfo;
import cn.tnar.cloud.parkservice.model.service.ParkInfoService;
import cn.tnar.cloud.parkservice.util.ResultJson;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author dzx
 * @ClassName:
 * @Description:
 * @date 2019年04月09日 09:49:18
 */
@RestController
@RequestMapping("/park")
@Api(tags = "ParkInfoController 段朝旭")
public class ParkInfoController {

    @Autowired
    private ParkInfoService parkInfoService;

    @Autowired
    private AcctMemberInfoFeign acctMemberInfoFeign;

    @GetMapping("/index")
    @ApiOperation("停车场index信息")
    public String index() {
        return "停车场服务";
    }

    @GetMapping("/getById")
    @ApiOperation("根据id查询停车场")
    @HystrixCommand(fallbackMethod = "getByIdFallBack", threadPoolProperties = {
            @HystrixProperty(name = "coreSize", value = "10"), @HystrixProperty(name = "maxQueueSize", value = "100"),
            @HystrixProperty(name = "queueSizeRejectionThreshold", value = "20")}, commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000")})
    public ResultJson getById(@RequestParam(defaultValue = "123742") Long id) {
        try {
            return ResultJson.getInstance().setCode(200).setData(parkInfoService.selectOneById(id)).setMsg("success");
        } catch (Exception e) {
            return ResultJson.getInstance().setCode(400).setMsg("error");
        }
    }

    public ResultJson getByIdFallBack(@RequestParam(defaultValue = "123742") Long id) {
        ParkInfo parkInfo = new ParkInfo();
        parkInfo.setId(00000L);
        return ResultJson.getInstance().setCode(400).setData(parkInfo).setMsg("error");
    }

    /**
     * mybatis plus 测试
     */
    @GetMapping("/selectByPage")
    @ApiOperation("分页查询")
    public ResultJson selectByPage(int current, int size) {
        return ResultJson.getInstance().setCode(200).setData(parkInfoService.page(new Page<ParkInfo>(current, size), new QueryWrapper<ParkInfo>()
                .orderByDesc("id"))).setMsg("success");
    }


    /*----调用用户服务---*/
    @GetMapping("/memberIndex")
    @ApiOperation("调用用户服务-获取用户index信息")
    public String memberIndex() {
        return acctMemberInfoFeign.index();
    }

    @GetMapping("/getMemberInfoById")
    @ApiOperation("调用用户服务-根据id查询用户")
    public ResultJson getMemberInfoById(@RequestParam(defaultValue = "131881") Long id) {
        return acctMemberInfoFeign.getById(id);
    }


}
